import { createRouter, createWebHistory } from 'vue-router'
import LayOut from '../views/LayOut/LayOut.vue'
import store from "../store/index.js"
const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/pages/login.vue")
  },
  {
    path: "/",
    name: "layout",
    component: LayOut,
    redirect:"/index",
    children:[
      {
        path:"/user",
        name:"user",
        component:()=>import("../views/pages/superman/users.vue")
      },
      {
        path:"/role",
        name:"role",
        component:()=>import("../views/pages/superman/roles.vue")
      },
      {
        path:"/privilege",
        name:"privilege",
        component: () => import("../views/pages/superman/privileges.vue")
      },
      {
        path:"/information",
        name:"information",
        component: () => import("../views/pages/superman/information.vue")
      },
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
//未登录跳转登录
router.beforeEach((to, form, next) => {
  /**
   * to:从哪个页面
   * from:到哪个页面
   * next:只有执行next()页面才会进行跳转
   */
  // 判断用户是否登录
  console.log("store", store.state.uInfo)
  const uInfo = store.state.uInfo.userInfo
  if (!uInfo.username) {
    // 未登录,跳转到login 
    if (to.path === "/login") {
      next()
      return
    }
    next("/login")

  } else {

    next()
  }

})
export default router
